package com.zgw.his.design.common;

import java.io.File;

import org.eclipse.core.internal.resources.Resource;
import org.eclipse.core.internal.resources.Workspace;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wb.internal.core.utils.IOUtils2;

/**
 * 本地资源帮助
 * */
@SuppressWarnings("restriction")
public class LocalResourceHelp {
	public final static Workspace WORKSPACE = (Workspace) ResourcesPlugin
			.getWorkspace();
	public final static String WORKSPACE_PATH = Platform.getLocation()
			.toString();

	/**
	 * @param type
	 *            {@link IResource FILE|FOLDER}
	 * */
	public static Resource getResource(String path, int type) {
		return WORKSPACE.newResource(new Path(path), type);
	}

	public static String getWorkspacePath() {
		String rootPath = WORKSPACE.getMetaArea().getLocation().toString();
		File workspaceFile = new File(rootPath.substring(0,
				rootPath.indexOf(".metadata")));
		return workspaceFile.getPath();
	}

	/**
	 * 在本地创建一个 IFolder
	 * 
	 * @throws Exception
	 * */
	public static IFolder createIFolder(String pathTxt) throws Exception {
		IPath path = new Path(pathTxt);
		IFolder folder = (IFolder) WORKSPACE
				.newResource(path, IResource.FOLDER);
		if (folder.exists()) {
			throw new Exception("文件 已存在:" + pathTxt);
		}
		folder.create(true, true, null);
		return folder;
	}

	public static boolean fileExists(String pathTxt) {
		IPath path = new Path(pathTxt);
		IFile file = (IFile) WORKSPACE.newResource(path, IResource.FILE);
		return file.exists();
	}

	public static boolean folderExists(String pathTxt) {
		IPath path = new Path(pathTxt);
		IFolder folder = (IFolder) WORKSPACE
				.newResource(path, IResource.FOLDER);
		return folder.exists();
	}

	/**
	 * 在本地创建一个 IFile
	 * 
	 * @throws Exception
	 * */
	public static IFile createIFile(String pathTxt, String defaultContent)
			throws Exception {
		IPath path = new Path(pathTxt);
		IFile xwtfile = (IFile) WORKSPACE.newResource(path, IResource.FILE);
		if (xwtfile.exists()) {
			throw new Exception("文件 已存在:" + pathTxt);
		}
		IOUtils2.setFileContents(xwtfile, defaultContent);
		return xwtfile;
	}

	public static IFile getFile(String path) {
		return (IFile) WORKSPACE.newResource(new Path(path), IResource.FILE);
	}

	public static IFolder getFolder(String path) {
		return (IFolder) WORKSPACE
				.newResource(new Path(path), IResource.FOLDER);
	}

	public static void move(String path, String newPath) throws Exception {
		IFile f = getFile(path);
		f.move(new Path(newPath), true, null);
	}

	public static void deleteResource(Resource resource) throws CoreException {
		resource.delete(true, false, null);
	}
}
